home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b14 / Interface / WindowInt.c < prev    next >
Text File  |  1996-04-11  |  11KB  |  538 lines

  1. /*****
  2.  *
  3.  *    WindowInt.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/grant/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16. #if kCompileWithForeground
  17.  
  18. #include "compiler_stuff.h"
  19. #include "globals.h"
  20.  
  21. #include "AboutBox.h"
  22. #include "DebugUtil.h"
  23. #include "MemoryUtil.h"
  24. #include "MenuFunc.h"
  25. #include "ProcessUtil.h"
  26.  
  27. #include "WindowInt.h"
  28.  
  29.  
  30. /***  LOCAL PROTOTYPES ***/
  31.  
  32. static    Boolean        windowValid                ( WindowPtr );
  33. static    Boolean        windowHasScrollbars        ( WindowPtr );
  34. static    void        invalidateScrollbars    ( WindowPtr );
  35.  
  36.  
  37. /***  FUNCTIONS  ***/
  38.  
  39. /* MENU SELECTION HANDLING */
  40.  
  41. /*  */
  42. void
  43. WindowMenuClose ( short modifiers )
  44. {
  45.     WindowClose ( NULL, modifiers, true );
  46. } /* WindowMenuClose */
  47.  
  48.  
  49. /* EVENT HANDLING */
  50.  
  51. /* I think that state should be true if it is activating, false if deactivating.
  52.     IM-MTE: 2-53,54 */
  53. void
  54. WindowActivate ( WindowPtr theWindow, Boolean activate, const EventRecord *theEvent )
  55. {
  56.     window_type        windType;
  57.     
  58.     windType = WindowType ( theWindow );
  59.     
  60.     /* restore or hide/reduce-to-outline window's selections */
  61.     switch ( windType )
  62.     {
  63.         #if kCompileWithApplicationWindows
  64.         case Window_Application :
  65.             CustomActivateWindow ( theWindow, activate );
  66.             break;
  67.         #endif
  68.         
  69.         case Window_dlgModal :
  70.         case Window_dlgMoveableModal :
  71.         case Window_about :
  72.         case Window_DA :
  73.         case Window_UNKNOWN :
  74.         default :
  75.             break;
  76.     }
  77.     
  78.     if ( activate )
  79.     {
  80.         /* show scroll bars if necessary */
  81.         
  82.         adjustMenus ();
  83.     }
  84.     else
  85.     {
  86.         /* hide scroll bars if necessary */
  87.     }
  88. } /* WindowActivate */
  89.  
  90.  
  91. /*  */
  92. void
  93. WindowUpdate ( WindowPtr theWindow )
  94. {
  95.     GrafPtr            oldPort;
  96.     window_type        windowType;
  97.     
  98.     windowType    = WindowType ( theWindow );
  99.     if ( !(windowType == Window_DA) )
  100.     {
  101.         GetPort        ( &oldPort );
  102.         SetPort        ( (GrafPtr)theWindow );
  103.         BeginUpdate    ( theWindow );
  104.  
  105.         switch ( windowType )
  106.         {
  107.             case Window_about :
  108.                 AboutBoxUpdate ();
  109.                 break;
  110.             
  111.             case Window_UNKNOWN :
  112.                 my_assert ( kForceAssert, "\pWindowUpdate: Unknown window type" );
  113.                 break;
  114.             
  115.             case Window_dlgModal :
  116.             case Window_dlgMoveableModal :
  117.             case Window_none :
  118.                 break;
  119.             
  120.             #if kCompileWithApplicationWindows
  121.             case Window_Application :
  122.             #endif
  123.             default :
  124.                 /* call custom window update procedure */
  125.                 #if kCompileWithApplicationWindows
  126.                 CustomUpdateWindow ( theWindow );
  127.                 #endif
  128.                 break;
  129.         }
  130.         
  131.         EndUpdate    ( theWindow );
  132.         SetPort        ( oldPort );
  133.     }
  134. } /* WindowUpdate */
  135.  
  136.  
  137. /* MOUSE CLICK HANDLING */
  138.  
  139. /* see the control manager chapter in IM-MTE for an example DoContentClick procedure
  140.     was doInContent */
  141. void 
  142. WindowContentClick ( WindowPtr theWindow, const EventRecord *eventPtr, Boolean *onItem )
  143. {
  144.     Point            whereClicked;
  145.     window_type        windowType;
  146.     GrafPtr            savePort;
  147.     
  148.     whereClicked = eventPtr->where;
  149.     
  150.     GetPort            ( &savePort );
  151.     SetPort            ( (GrafPtr)theWindow );
  152.     GlobalToLocal    ( &whereClicked );
  153.     SetPort            ( savePort );
  154.     
  155.     windowType = WindowType ( theWindow );
  156.     
  157.     switch ( windowType )
  158.     {
  159.         case Window_UNKNOWN :
  160.             my_assert ( kForceAssert, "\pWindowClose: unknown window type" );
  161.             break;
  162.         
  163.         case Window_about :
  164.         case Window_dlgModal :
  165.         case Window_dlgMoveableModal :
  166.         case Window_DA :
  167.             break;
  168.             
  169.         /* NOTE: unrecognized windows will be passed to the application
  170.             for clicks. */
  171.         #if kCompileWithApplicationWindows
  172.         case Window_Application :
  173.         #endif
  174.         default :
  175.             #if kCompileWithApplicationWindows
  176.             CustomClickInWindow ( theWindow, whereClicked, onItem );
  177.             #endif
  178.             break;
  179.     }
  180. } /* WindowContentClick */
  181.  
  182.  
  183. /* was doZoomBox */
  184. void
  185. WindowZoomBox ( WindowPtr theWindow, short partCode )
  186. {
  187.     GrafPtr    savePort;
  188.     
  189.     GetPort ( &savePort );
  190.     SetPort ( theWindow );
  191.  
  192.     EraseRect ( &theWindow->portRect );
  193.     ZoomWindow ( theWindow, partCode, false );
  194.  
  195.     invalidateScrollbars ( theWindow );
  196.     
  197.     /* setup for redraw */
  198.     InvalRect ( &theWindow->portRect );
  199.  
  200.     SetPort ( savePort );
  201. } /* WindowZoomBox */
  202.  
  203.  
  204. /* was doGrowWindow */
  205. void 
  206. WindowGrow ( WindowPtr theWindow, Point thePoint )
  207. {
  208.     long    newSize;
  209.     Rect    limitRect;
  210.     
  211.     /* set maximum size of window to entire monitor(s) area */
  212.     limitRect        = gGrayRgnRect;
  213.     /* set minimum size of window */
  214.     limitRect.left    = kMinWindSize;
  215.     limitRect.top    = kMinWindSize;
  216.     
  217.     /* track user mouse and grow the window */
  218.     newSize = GrowWindow ( theWindow, thePoint, &limitRect );
  219.     if ( newSize != nil )
  220.     {
  221.         /* erase and invalidate scroll bar area */
  222.         invalidateScrollbars ( theWindow );
  223.         
  224.         SizeWindow ( theWindow, LoWord(newSize), HiWord(newSize), true );
  225.  
  226.         /* relocate scroll bars, then erase and ivalidate scroll bar area */
  227.         invalidateScrollbars ( theWindow );
  228.     }
  229. } /* WindowGrow */
  230.  
  231.  
  232. /*  */
  233. #pragma mark -
  234.  
  235. /* was doCloseWindow */
  236. Boolean
  237. WindowClose ( WindowPtr theWindow, short modifiers, Boolean userInteract )
  238. {
  239.     WindowPtr    windowToClose;
  240.     Boolean        theWindowClosed;
  241.     #if kCompileWithApplicationWindows
  242.     OSErr        theErr;
  243.     #endif
  244.     
  245.     /* the window ptr must either be NULL or a valid window */
  246.     my_assert ( (theWindow == NULL) || (windowValid(theWindow)),
  247.         "\pWindowClose: invalid window" );
  248.     
  249.     /* assume function unsuccessful until a window is acutally closed */
  250.     theWindowClosed = false;
  251.     
  252.     if ( modifiers & optionKey )
  253.     {
  254.         /* if the optionKey is active, try to close all open windows */
  255.         theWindowClosed = WindowCloseAll ( userInteract );
  256.     }
  257.     else
  258.     {
  259.         if ( theWindow == NULL )
  260.         {
  261.             windowToClose = FrontWindow ();
  262.         }
  263.         else
  264.         {
  265.             windowToClose = theWindow;
  266.         }
  267.         
  268.         /* do the appropriate type of close based on the kind of window */
  269.         switch ( WindowType( windowToClose ) )
  270.         {
  271.             case Window_about :
  272.                 AboutBoxClose ();
  273.                 theWindowClosed = true;
  274.                 break;
  275.             
  276.             case Window_UNKNOWN :
  277.                 my_assert ( kForceAssert, "\pWindowClose: unknown window type" );
  278.                 break;
  279.             
  280.             case Window_dlgModal :
  281.             case Window_dlgMoveableModal :
  282.             case Window_DA :
  283.                 break;
  284.                 
  285.             /* NOTE: unrecognized windows will be passed to the application
  286.                 to close. */
  287.             #if kCompileWithApplicationWindows
  288.             case Window_Application :
  289.             #endif
  290.             default :
  291.                 #if kCompileWithApplicationWindows
  292.                 theErr = CustomCloseWindow ( windowToClose );
  293.                 theWindowClosed = (theErr == noErr);
  294.                 #endif
  295.                 break;
  296.         }
  297.     }
  298.     
  299.     return theWindowClosed;
  300. } /* WindowClose */
  301.  
  302.  
  303. /* was closeAllWindows */
  304. Boolean
  305. WindowCloseAll ( Boolean userInteract )
  306. {
  307.     Boolean        theWindowsClosed;
  308.     WindowPtr    theWindow;
  309.     
  310.     theWindowsClosed = true;
  311.     theWindow = FrontWindow ();
  312.     if ( theWindow != nil )
  313.     {
  314.         /* keep closing windows until they are all closed, or the user interrupts
  315.             or cancels the closing */
  316.         do
  317.         {
  318.             theWindowsClosed = WindowClose ( theWindow, nil, userInteract );
  319.             theWindow         = FrontWindow ();
  320.             
  321.             /* give other processes time */
  322.             ProcessGiveTime ( nil );
  323.         } while ( (theWindow != NULL) && (!userInteract || theWindowsClosed) );
  324.     }
  325.     
  326.     return theWindowsClosed;
  327. } /* WindowCloseAll */
  328.  
  329.  
  330. /* WINDOW KIND TESTING */
  331. #pragma mark -
  332.  
  333. /* was kindOfWindow */
  334. window_type
  335. WindowType ( WindowPtr theWindow )
  336. {
  337.     int                windowKind;
  338.     windowInfoHdl    windInfo;
  339.     
  340.     /* if the window is a null pointer return negative */
  341.     if ( theWindow == NULL )
  342.     {
  343.         return Window_none;
  344.     }
  345.     
  346.     windInfo = (windowInfoHdl) GetWRefCon ( theWindow );
  347.     if ( windInfo != NULL )
  348.     {
  349.         return (*windInfo)->wType;
  350.     }
  351.     
  352.     windowKind = ((WindowPeek)theWindow)->windowKind;
  353.     if ( windowKind < nil )
  354.     {
  355.         /* desk accessory */
  356.         return Window_DA;
  357.     }
  358.     else if ( windowKind == dialogKind )
  359.     {
  360.         return Window_dlgModal;
  361.     }
  362.     else
  363.     {
  364.         return Window_UNKNOWN;
  365.     }
  366. } /* WindowType */
  367.  
  368.  
  369. /*  */
  370. Boolean
  371. WindowNewSetInfo (
  372.     WindowPtr        theWindow,
  373.     window_type        theType,
  374.     Boolean            hasScrollbars,
  375.     long            theData )
  376. {
  377.     windowInfoHdl    theWindInfo;
  378.     
  379.     theWindInfo = WindowNewInfoHdl ( theType, hasScrollbars, theData );
  380.     
  381.     WindowSetInfoHdl ( theWindow, theWindInfo );
  382.     
  383.     return ( theWindInfo != NULL );
  384. } /* WindowNewSetInfo */
  385.  
  386.  
  387. /*  */
  388. windowInfoHdl
  389. WindowNewInfoHdl (
  390.     window_type        theType,
  391.     Boolean            hasScrollbars,
  392.     long            theData )
  393. {
  394.     windowInfoHdl    theWindInfo;
  395.     
  396.     theWindInfo = (windowInfoHdl) MemoryNewHandle ( sizeof(windowInfo), NULL );
  397.     if ( theWindInfo != NULL )
  398.     {
  399.         (*theWindInfo)->wType        = theType;
  400.         (*theWindInfo)->scrollbars    = hasScrollbars;
  401.         (*theWindInfo)->data        = theData;
  402.     }
  403.     
  404.     return theWindInfo;
  405. } /* WindowNewInfoHdl */
  406.  
  407.  
  408. /*  */
  409. void
  410. WindowSetInfoHdl ( WindowPtr theWindow, windowInfoHdl theWindInfo )
  411. {
  412.     SetWRefCon ( theWindow, (long)theWindInfo );
  413. } /* WindowSetInfoHdl */
  414.  
  415.  
  416. /*  */
  417. windowInfoHdl
  418. WindowGetInfoHdl ( WindowPtr theWindow )
  419. {
  420.     windowInfoHdl    theWindInfo;
  421.     
  422.     theWindInfo = (windowInfoHdl) GetWRefCon ( theWindow );
  423.     
  424.     return theWindInfo;
  425. } /* WindowGetInfoHdl */
  426.  
  427.  
  428. /* Return the user specified data from the windowInfoHdl of the window */
  429. long
  430. WindowGetData ( WindowPtr theWindow )
  431. {
  432.     windowInfoHdl    theWindInfo;
  433.     
  434.     theWindInfo = (windowInfoHdl) GetWRefCon ( theWindow );
  435.     if ( theWindInfo == NULL )
  436.     {
  437.         return nil;
  438.     }
  439.     else
  440.     {
  441.         return (*theWindInfo)->data;
  442.     }
  443. } /* WindowGetData */
  444.  
  445.  
  446. /* Return the user specified data from the windowInfoHdl of the window, as a handle
  447.     For backwards compatibility. Avoid using this function. */
  448. Handle
  449. WindowGetDataHdl ( WindowPtr theWindow )
  450. {
  451.     return (Handle) (WindowGetData(theWindow));
  452. } /* WindowGetDataHdl */
  453.  
  454.  
  455. /*  */
  456. void
  457. WindowDisposeInfoHdl ( WindowPtr theWindow )
  458. {
  459.     Handle    theWindInfo;
  460.     
  461.     theWindInfo = (Handle) GetWRefCon ( theWindow );
  462.     if ( theWindInfo != NULL )
  463.     {
  464.         DisposeHandle ( theWindInfo );
  465.     }
  466. } /* WindowDisposeInfoHdl */
  467.  
  468.  
  469. /* MISC UTILITIES */
  470.  
  471. /*  */
  472. static Boolean
  473. windowValid ( WindowPtr theWindow )
  474. {
  475.     Boolean        isValid;
  476.     
  477.     if ( theWindow == NULL )
  478.     {
  479.         isValid = false;
  480.     }
  481.     else
  482.     {
  483.         /* need more tests here */
  484.         isValid = true;
  485.     }
  486.     
  487.     return isValid;
  488. } /* windowValid */
  489.  
  490.  
  491. /*  */
  492. static Boolean
  493. windowHasScrollbars ( WindowPtr theWindow )
  494. {
  495.     windowInfoHdl    windInfo;
  496.     
  497.     windInfo = (windowInfoHdl) GetWRefCon ( theWindow );
  498.     if ( windInfo != NULL )
  499.     {
  500.         return (*windInfo)->scrollbars;
  501.     }
  502.     else
  503.     {
  504.         return false;
  505.     }
  506. } /* windowHasScrollbars */
  507.  
  508.  
  509. /* Invalidate the graphic region encompasing the scrollbars so that they will be
  510.     properly updated. */
  511. static void
  512. invalidateScrollbars ( WindowPtr theWindow )
  513. {
  514.     Rect    tempRect;
  515.     
  516.     if ( windowHasScrollbars(theWindow) )
  517.     {
  518.         SetPort ( theWindow );
  519.         
  520.         /* invalidate & erase vertical (right-hand side) scrollbar */
  521.         tempRect        = theWindow->portRect;
  522.         tempRect.left    = tempRect.right-15;
  523.         InvalRect ( &tempRect );
  524.         EraseRect ( &tempRect );
  525.         
  526.         /* invalidate & erase horizontal (bottom) scrollbar */
  527.         tempRect        = theWindow->portRect;
  528.         tempRect.top    = tempRect.bottom-15;
  529.         InvalRect ( &tempRect );
  530.         EraseRect ( &tempRect );
  531.     }
  532. } /* invalidateScrollbars */
  533.  
  534.  
  535. #endif    /* kCompileWithForeground */
  536.  
  537. /*****  EOF  *****/
  538.